package com.hlz.lib.effectivejava.threadlocal;

/**
 * @description:
 * @author: Castile
 * @create: 2024-04-05 15:40
 * @Version 1.0
 **/
public class NullableThreadLocal {

    public static void main(String[] args) {
        final InheritableThreadLocal<Integer> itl = new InheritableThreadLocal<Integer>(){

            @Override
            protected Integer childValue(Integer parentValue) {
                if (parentValue == null){
                    return 0;
                }
                return parentValue * 2;
            }

            @Override
            protected Integer initialValue() {
                return 1;
            }
        };

        // test
        System.out.println(Thread.currentThread().getName() + ": " + itl.get());
        itl.set(null);

        new Thread(()->{
            System.out.println(Thread.currentThread().getName() + ": " + itl.get());

        }, "child-thread").start();

    }
}
